#include "person.pb-c.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

static void print_person(Test__Person* person)
{
	printf("id: %d\n", person->id);
	printf("name: %s\n", person->name);
	printf("email: %s\n", person->email);
}
int main()
{
  Test__Person person = TEST__PERSON__INIT;
  Test__Person* personShow = NULL;
  void* buf = NULL;
  person.id = 1000;
  person.name = "Alice";
  person.email = "Alice@example.com";

  // get person packed size
  size_t len;
  len = test__person__get_packed_size(&person);
  printf("size of person : %u \n", len);
  buf = malloc(len);

  struct timeval t_start,t_end;
  gettimeofday(&t_start, NULL);
  long start = ((long)t_start.tv_sec)*1000+(long)t_start.tv_usec/1000;
  int i;
  for(i=0; i< 100000; i++)
  {
	  // put person into buf
	  test__person__pack(&person, buf);
	  // unpack from buf
	  personShow = test__person__unpack(NULL, len, buf);
  }
  gettimeofday(&t_end, NULL);
  long end = ((long)t_end.tv_sec)*1000+(long)t_end.tv_usec/1000;

  long cost_time = end - start;
  printf("cost_time: %ld ms\n", cost_time);

  print_person(personShow);

  // free personShow
  test__person__free_unpacked(personShow, NULL);

  free(buf);

  return 0;
}
